package tech.xs.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import tech.xs.pojo.ClaimVoucher;
import tech.xs.pojo.ClaimVoucherDetail;
import tech.xs.pojo.Employee;
import tech.xs.service.ClaimVoucherDetailService;
import tech.xs.service.ClaimVoucherService;
import tech.xs.service.EmployeeService;
import tech.xs.util.PaginationSupport;

public class ClaimVoucherAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	@Autowired
	private ClaimVoucherService claimVoucherService;

	@Autowired
	private EmployeeService employeeService;

	@Autowired
	private ClaimVoucherDetailService claimVoucherDetailService;
	Map<String, String> statusMap = new HashMap<>();
	private long id;
	private int pageNo = 1;
	private int pageSize = 5;
	private ClaimVoucher claimVoucher;
	private Employee employee;
	private Date startDate;
	private Date endDate;
	private ClaimVoucherDetail claimVoucherDetail;
	private List<ClaimVoucherDetail> detailList;
	PaginationSupport<ClaimVoucher> pageSupport = new PaginationSupport<>();

	public String list() {
		try {
			Map<String, Object> session = ActionContext.getContext().getSession();
			Employee sysEmployee = (Employee) session.get("employee");
			if (claimVoucher == null) {
				claimVoucher = new ClaimVoucher();
			}
			int count = claimVoucherService.count(sysEmployee, claimVoucher, startDate, endDate);
			pageSupport.setTotalCount(count);
			pageSupport.setCurrPageNo(pageNo);
			pageSupport.setPageSize(pageSize);
			List<ClaimVoucher> list = claimVoucherService.findlist(sysEmployee, claimVoucher, startDate, endDate,
					((pageSupport.getCurrPageNo() - 1) * pageSize), pageSupport.getPageSize());
			pageSupport.setItems(list);
			List<String> statusMap1 = claimVoucherService.selectStatus();
			statusMap = new HashMap<>();
			for (int i = 0; i < statusMap1.size(); i++) {
				statusMap.put(statusMap1.get(i), statusMap1.get(i));
			}
			session.put("pageSupport", pageSupport);
			return "list";
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("报错");
			return ERROR;
		}
	}

	public String delete() {
		System.out.println("delete-----------");
		claimVoucher = claimVoucherService.findById(claimVoucher.getId());	
		System.out.println(claimVoucher+"------------");
		setDetailList(claimVoucher.getDetailList());
		// 先删除详细、再删除本体
		for (ClaimVoucherDetail detailLists : detailList) {
			claimVoucherDetailService.deleteDetail(detailLists);
		}
		claimVoucherService.delete(claimVoucher);
		return SUCCESS;
	}

	// 进入toAdd页面
	public String toAdd() {
		return SUCCESS;
	}

	// Add
	public String add() {
		@SuppressWarnings("rawtypes")
		Map session = ActionContext.getContext().getSession();
		Employee employee = (Employee) session.get("employee");

		claimVoucher.setCreator(employee);// 设置填报人

		if (claimVoucher.getStatus().equals("已提交")) {
			// 状态是已提交,下一处理人是经理
			employee = employeeService.findById("002");
			claimVoucher.setCreator(employee);
		} else {
			claimVoucher.setCreator(employee);
		}

		claimVoucher.setCreateTime(new Date());

		System.out.println("提交前claimVoucher+++++++++++++++++++++++++++++");
		boolean falg = claimVoucherService.save(claimVoucher);
		System.out.println("提交后claimVoucher+++++++++++++++++++++++++++++");
		/*
		 * System.out.println("提交前detailList+++++++++++++++++++++++++++++"); falg =
		 * claimVoucherDetailService.save(detailList);
		 * System.out.println("提交后detailList+++++++++++++++++++++++++++++");
		 */
		if (falg) {
			this.addActionMessage("添加报销单成功！");
			return SUCCESS;
		}
		this.addActionMessage("添加报销单失败！");
		return INPUT;
		
	}

	// 进入更新页面
	public String toedit() {
		claimVoucher = claimVoucherService.findById(claimVoucher.getId());
		System.out.println(claimVoucher + "------------------------");
		if (claimVoucher != null) {
			return SUCCESS;
		}
		return "input";
	}

	// 更新
	public String update() {
		Map<String, Object> map = ActionContext.getContext().getSession();
		Employee employee = (Employee) map.get("employee");
		ClaimVoucher newClaimVoucher = claimVoucherService.findById(claimVoucher.getId());
		newClaimVoucher.setCreator(employee);
		if (claimVoucher.getStatus().equals("已提交")) {
			// 状态是已提交,下一处理人是经理
			employee = employeeService.findById("002");
			newClaimVoucher.setNextDeal(employee);
		} else {
			newClaimVoucher.setNextDeal(employee);
		}
		newClaimVoucher.setModifyTime(new Date());
		newClaimVoucher.setEvent(claimVoucher.getEvent());
		newClaimVoucher.setStatus(claimVoucher.getStatus());
		newClaimVoucher.setTotalAccount(claimVoucher.getTotalAccount());

		newClaimVoucher.setDetailList(detailList);
		System.out.println(newClaimVoucher);
		boolean flag = claimVoucherService.update(newClaimVoucher);
		if (flag) {
			for (int i = 0; i < detailList.size(); i++) {
				ClaimVoucherDetail detail = detailList.get(i);
				if (detail.getId() == 0) {
					detail.setBizClaimVoucher(newClaimVoucher);
				} else {
					detailList.remove(i);
					i--;
				}
			}
		}
		System.out.println(detailList);
		flag = claimVoucherDetailService.save(detailList);
		if (flag) {
			this.addActionMessage("修改报销单成功！");
		} else {
			this.addActionMessage("修改报销单失败！");
		}

		return SUCCESS;
	}

	public ClaimVoucherService getClaimVoucherService() {
		return claimVoucherService;
	}

	public void setClaimVoucherService(ClaimVoucherService claimVoucherService) {
		this.claimVoucherService = claimVoucherService;
	}

	public Map<String, String> getStatusMap() {
		return statusMap;
	}

	public void setStatusMap(Map<String, String> statusMap) {
		this.statusMap = statusMap;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public ClaimVoucher getClaimVoucher() {
		return claimVoucher;
	}

	public void setClaimVoucher(ClaimVoucher claimVoucher) {
		this.claimVoucher = claimVoucher;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public ClaimVoucherDetail getClaimVoucherDetail() {
		return claimVoucherDetail;
	}

	public void setClaimVoucherDetail(ClaimVoucherDetail claimVoucherDetail) {
		this.claimVoucherDetail = claimVoucherDetail;
	}

	public List<ClaimVoucherDetail> getDetailList() {
		return detailList;
	}

	public void setDetailList(List<ClaimVoucherDetail> detailList) {
		this.detailList = detailList;
	}

	public PaginationSupport<ClaimVoucher> getPageSupport() {
		return pageSupport;
	}

	public void setPageSupport(PaginationSupport<ClaimVoucher> pageSupport) {
		this.pageSupport = pageSupport;
	}
}
